package core;

import java.util.ArrayList;
import java.util.List;

import persistence.dto.StorageUnitDTO;

import core.reports.IVisitor;
import core.reports.TraversalOrder;


/**
 * This Describes a Storage Unit object.  This holds products and
 * other ProductGroups. This is just a specialization of a Product
 * Container.  
 * 
 * @author Spencer Hoffa
 *
 */
public class StorageUnit extends ProductContainer
{
	/** Serializable ID
	 * 
	 */
	private static final long serialVersionUID = -1452745116687579804L;
	
	/** pointer to RootStorageUnit root class */
	private RootStorageUnit root;
	
	private boolean updateFlag;
	
	/**
	 * create  a new storage unit dto
	 * @return returns a new storage unit dto
	 */
	public StorageUnitDTO createDTO()
	{
		return new StorageUnitDTO(getID(),name);
	}
	
	/**
	 * Constructor for StorageUnit.
	 * @param name The name of this StorageUnit
	 * @throws IllegalArgumentException thrown when "name" parameter is null of empty
	 */
	public StorageUnit(String name) throws IllegalArgumentException
	{
		super();
		assert(name != "");
		
		if(name == null || name == "" )
			throw new IllegalArgumentException();
		this.name = name;
		this.root = RootStorageUnit.getInstance();
		this.groups = new ArrayList<ProductGroup>();
		this.updateFlag = false;
	}
	
	public void setUpdateFlag(boolean val)
	{
		if(val == true)
			updateFlag = true;
		else
			updateFlag = false;
	}
	
	public boolean getUpdateFlag()
	{
		return updateFlag;
	}
	
	/** Returns the type of the StorageUnit, which should be
	 * ContainerType.STORAGE_UNIT
	 * @return the ContainerType of the class
	 */
	@Override
	public ContainerType getContainerType()
	{
		return ContainerType.STORAGE_UNIT;
	}

	/** Modifies calling storage unit with the "after" StorageUnit
	 * 
	 * @param after the StorageUnit type which defines the new attributes to be copied
	 * @return returns a copy of the calling StorageUnit, with new attributes set
	 * @throws IllegalArgumentException if after is null
	 */
	public StorageUnit editStorageUnit(StorageUnit after) throws IllegalArgumentException
	{
		assert(after !=null);
		
		if(after == null)
			throw new IllegalArgumentException();
		
		this.setName(after.getName());
		
		return this;
	}

	@Override
	public void accept(IVisitor v, TraversalOrder order)
	{
		if (order == TraversalOrder.PreOrder)
		{
			v.visitStorageUnit(this, order);
			traverseChildren(v, order);
		}
		else if (order == TraversalOrder.PostOrder)
		{
			traverseChildren(v, order);
			v.visitStorageUnit(this, order);
		}
		else
		{
			//this should be possible to get to
			System.out.println("Error in traversing Tree: Unidentified Order.");
		}
	}

	
	
}
